﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace OnlineManagement.Web.Account
{
	public partial class Login : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            btn.Click += new EventHandler(btn_Click);
            if(HttpContext.Current.User.Identity.IsAuthenticated){

                Response.Redirect(FormsAuthentication.GetRedirectUrl(HttpContext.Current.User.Identity.Name,false));
            }
            
           // Server.Transfer("/");
		}



        void btn_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(username.Value)) { 
                    tips_username.InnerHtml="<div class=\"login_prompt\" id=\"tips_username\"><dl>帐号不能为空</dl></div>";
                    return;
                }
                if(string.IsNullOrEmpty(password.Value)){
                    tips_password.InnerHtml = "<div class=\"login_prompt\" id=\"tips_password\"><dl>密码不能为空</dl></div>";
                    return;
                }
                FormsAuthentication.Initialize();
                bool result = Membership.ValidateUser(username.Value, password.Value);
                if (result)
                {
                    BLL.TempRegist loginDll = new BLL.TempRegist();
                    int hoggeryId = loginDll.GetHoggeryIdForLogin(username.Value);
                    FormsAuthenticationTicket ticket = null;
                    if (coks.Checked)
                    {
                        ticket = new FormsAuthenticationTicket(1,
                                          username.Value,
                                          DateTime.Now,
                                          DateTime.Now.AddDays(14),
                                          true,
                                          hoggeryId.ToString(),//用户所属的角色字符串 
                                          FormsAuthentication.FormsCookiePath);
                    }
                    else
                    {
                        ticket = new FormsAuthenticationTicket(1,
                                              username.Value,
                                              DateTime.Now,
                                              DateTime.Now.AddMinutes(30),
                                              true,
                                              hoggeryId.ToString(),//用户所属的角色字符串 
                                              FormsAuthentication.FormsCookiePath);
                    }
                    string hash = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
                    if (ticket.IsPersistent)
                    {
                        cookie.Expires = ticket.Expiration;
                    }
                    //把准备好的cookie加入到响应流中 
                    Response.Cookies.Add(cookie);
                   
                    ////转发到请求的页面 
                    Response.Redirect(FormsAuthentication.GetRedirectUrl(username.Value, false));
                    
                }
                else
                {
                    tips_password.InnerHtml = "<div class=\"login_prompt\" id=\"tips_password\"><dl>用户名或密码错误</dl></div>";
                }
            }catch(Exception ex){
                tips_password.InnerHtml = "<div class=\"login_prompt\" id=\"tips_password\"><dl>服务器内部错误，请稍后再试</dl></div>";
            }
        }
        
	}
}